//
//  YYPhotoCollectionViewCell.swift
//  CodeLibrary_Swift
//
//  Created by 陆文宇 on 2017/2/9.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

class YYPhotoCollectionViewCell: UICollectionViewCell {
    
    static var choosedCount: Int = 0

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var chooseImageView: UIImageView!
    @IBOutlet weak var chooseLabel: UILabel!
    var chooseNumber: Int = 0 {
        willSet {
            if newValue > 0 {
                if chooseNumber > 0 {
                    chooseLabel.text = "\(newValue)"
                } else {
                    chooseLabel.text = "\(newValue)"
                    chooseImageView.backgroundColor = UIColor.cyan
                    chooseImageView.transform = CGAffineTransform(scaleX: 0, y: 0)
                    UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: .curveEaseInOut, animations: { 
                        self.chooseImageView.transform = CGAffineTransform(scaleX: 1, y: 1)
                    }, completion: nil)
                }
            } else {
                chooseLabel.text = nil;
                chooseImageView.backgroundColor = UIColor.clear
            }
        }
    }
    var model: YYPhotoModel? {
        willSet {
            if let newValue = newValue {
                if newValue !== model {
                    imageView.image = newValue.image
                    chooseNumber = newValue.chooseNumber
                } else {
                    chooseNumber = newValue.chooseNumber
                }
            }
        }
    }
    weak var controller: YYPhotoCollectionViewController?

    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        chooseImageView.layer.cornerRadius = 10;
        chooseImageView.layer.borderWidth = 1;
        chooseImageView.layer.borderColor = UIColor.white.cgColor
        
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapAction(sender:)))
        chooseLabel.addGestureRecognizer(tap)
    }

    @objc func tapAction(sender: UITapGestureRecognizer?) -> Void {
        if chooseNumber > 0 {
            chooseNumber = 0
            YYPhotoCollectionViewCell.choosedCount -= 1
        } else {
            if YYPhotoCollectionViewCell.choosedCount >= YYPhotoHandler.maxPickCount {
                let alertC: UIAlertController = UIAlertController(title: nil, message: "你最多只能选择\(YYPhotoHandler.maxPickCount)张照片", preferredStyle: .alert)
                alertC.addAction(UIAlertAction(title: "知道了", style: .cancel, handler: nil))
                controller?.present(alertC, animated: true, completion: nil)
                return
            }
            YYPhotoCollectionViewCell.choosedCount += 1
            chooseNumber = YYPhotoCollectionViewCell.choosedCount;
        }
        controller?.chooseClosure(model!)
        model?.chooseNumber = chooseNumber;
    }
}
